<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="../mastertemplate/MasterPage.xhtml" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
	xmlns:c="http://java.sun.com/jsp/jstl/core">	
	<f:metadata>
		<f:viewParam name="selectedGroupId" value="#{UserGroupsBean.selectedGroupId}" />
	</f:metadata>
	<ui:define name="left">
		<h:form id="menuForm">
			<ui:insert name="menubar">
				<ui:include src="tools/privilegemenubar.xhtml" />
			</ui:insert>
		</h:form>
	</ui:define>
	<ui:define name="right">
		<h:form id="groupForm">
			<p:growl id="messages" />
			<p:panel header="#{msg.privilege_title}">
				<p:commandButton rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
					 id="addGroup" icon="ui-icon-plusthick" value="#{msg.general_add}" styleClass="tableHeaderButton"
					 update=":editForm" action="#{UserGroupsBean.addGroup}" oncomplete="PF('editDialog').show();">
					 <f:setPropertyActionListener target="#{UserGroupsBean.createNew}" value="#{true}" />
				</p:commandButton>
				<p:tooltip for="addGroup" value="#{msg.general_add_tooltip}"/>
				<p:dataTable var="userGroup" value="#{UserGroupsBean.userGroups}" paginatorPosition="bottom"
					paginator="true" rows="10" rowsPerPageTemplate="10,20,50">
					<f:facet name="header">
						<h:outputText value="#{msg.privilege_usergroups_header}" styleClass="tableHeader" />
					</f:facet>
					
					<p:column headerText="#{msg.table_groupid}" styleClass="groupSearchColumn" filterBy="#{userGroup.groupId}" filterMatchMode="contains" sortBy="#{userGroup.groupId}">
						<h:outputText value="#{userGroup.groupId}" />
					</p:column>
					<p:column headerText="#{msg.table_groupname}" styleClass="groupSearchColumn" filterBy="#{userGroup.groupName}" filterMatchMode="contains" sortBy="#{userGroup.groupName}">
						<h:outputText value="#{userGroup.groupName}" />
					</p:column>
					<p:column headerText="#{msg.table_actions}" style="text-align:center; min-width:90px">
						<h:panelGroup>
							<p:commandButton action="#{UserGroupsBean.selectGroup(userGroup)}" styleClass="smallButton"
								update=":groupForm" icon="ui-icon ui-icon-search" id="selectgroup"/>
							<p:tooltip for="selectgroup" value="#{msg.privilege_group_tooltip}"/>		
							<h:panelGroup rendered="#{UserBean.getAccessLevel() == 'WRITE'}">	
								<p:commandButton styleClass="smallButton" action="#{UserGroupsBean.selectGroup(userGroup)}"
									update=":editForm" icon="ui-icon ui-icon-pencil" id="editgroup" oncomplete="PF('editDialog').show()">
									<f:setPropertyActionListener target="#{UserGroupsBean.createNew}" value="#{false}" />
								</p:commandButton>
								<p:tooltip for="editgroup" value="#{msg.general_edit_tooltip}"/>
								<p:commandButton styleClass="smallButton" action="#{UserGroupsBean.selectGroup(userGroup)}"
									onclick="PF('confirmDelete').show()" icon="ui-icon ui-icon-trash" id="deletegroup"/>
								<p:tooltip for="deletegroup" value="#{msg.general_delete_tooltip}"/>
							</h:panelGroup>	
						</h:panelGroup>
					</p:column>																			
				</p:dataTable>

				<h:panelGroup rendered="#{! empty UserGroupsBean.selectedGroup.groupId}">
					<br />
					<br />

					<p:commandButton rendered="#{UserBean.getAccessLevel() == 'WRITE'}"
						id="addRole" icon="ui-icon-plusthick" value="#{msg.general_add}" 
						styleClass="tableHeaderButton" update=":editRoleForm" 
						oncomplete="PF('editRoleDialog').show()">
						<f:setPropertyActionListener target="#{UserGroupsBean.createNew}" value="#{false}" />
					</p:commandButton>
				</h:panelGroup>
				<p:tooltip for="addRole" value="#{msg.general_add_tooltip}"/>
				<p:dataTable id="entryTable" rendered="#{! empty UserGroupsBean.selectedGroup.groupId}" var="userRole" value="#{UserGroupsBean.selectedRoles}" paginatorPosition="bottom"
					paginator="true" rows="10" rowsPerPageTemplate="10,20,50">
					<f:facet name="header">		
						<h:outputText value="#{msg.privilege_userroles_header}: #{UserGroupsBean.selectedGroup.groupName}" styleClass="tableHeader" />
					</f:facet>				
					
					<p:column headerText="#{msg.table_roleid}" styleClass="groupSearchColumn" filterBy="#{userRole.roleId}" filterMatchMode="contains" sortBy="#{userRole.roleId}">
						<h:outputLink value="#{request.contextPath}/pages/privilege/roles.xhtml">
							<f:param name="selectedRoleId" value="#{userRole.roleId}"/>
							<h:outputText value="#{userRole.roleId}" />
						</h:outputLink>
					</p:column>
					<p:column headerText="#{msg.table_rolename}" styleClass="groupSearchColumn" filterBy="#{userRole.roleName}" filterMatchMode="contains" sortBy="#{userRole.roleName}">
						<h:outputText value="#{userRole.roleName}" />
					</p:column>		
					
					<p:column headerText="#{msg.table_actions}" rendered="#{UserBean.getAccessLevel() == 'WRITE'}" style="text-align:center">
						<h:panelGroup>					
							<p:commandButton styleClass="smallButton"
								action="#{UserGroupsBean.selectRole(userRole)}"
								onclick="PF('confirmRoleDelete').show()"
								icon="ui-icon ui-icon-trash"
								id="deleterole"/>
							<p:tooltip for="deleterole" value="#{msg.general_delete_tooltip}"/>		
						</h:panelGroup>
					</p:column>							
				</p:dataTable>
			</p:panel>
		</h:form>
	</ui:define>
	<ui:define name="content">
		<h:form id="deleteRoleForm">
			<p:confirmDialog header="#{msg.general_delete_header}" id="confirmRoleDelete1" modal="true" widgetVar="confirmRoleDelete" width="450" height="80" message="#{msg.confirmDialog_sure}">
				<div style="float: right">
					<p:commandButton value="#{msg.general_ok}" actionListener="#{UserGroupsBean.deleteRoleFromGroup}" 
						oncomplete="PF('confirmRoleDelete').hide()" update=":groupForm"/>
					<p:commandButton value="#{msg.general_cancel}" oncomplete="PF('confirmRoleDelete').hide()"/>
				</div>
			</p:confirmDialog>		
		</h:form>

		<p:dialog id="editRoleDialog1" header="#{msg.general_addedit_header}" modal="true" resizable="false"
			widgetVar="editRoleDialog">
			<h:form id="editRoleForm">
				<h:panelGrid id="editRoleTable" columns="2">
					<h:outputText value="#{msg.table_userrole}:" />	
					<h:selectOneMenu label="#{msg.table_userrole}" value="#{UserGroupsBean.selectedRoleName}">
							<f:selectItems value="#{UserGroupsBean.notSelectedRolesNames}"/>
					</h:selectOneMenu>											
				</h:panelGrid>
				<br />
				<div style="float: right">
					<p:commandButton value="#{msg.general_save}"
						action="#{UserGroupsBean.addRoleToGroup}"
						oncomplete="PF('editRoleDialog').hide()" update=":groupForm" />
				</div>
			</h:form>
		</p:dialog>	

		<h:form id="deleteForm">
			<p:confirmDialog header="#{msg.general_delete_header}" id="confirmDelete1" modal="true" widgetVar="confirmDelete" width="450" height="80" message="#{msg.confirmDialog_sure}">
				<div style="float: right">
					<p:commandButton value="#{msg.general_ok}" actionListener="#{UserGroupsBean.deleteGroup}" 
						oncomplete="PF('confirmDelete').hide()" update=":groupForm"/>
					<p:commandButton value="#{msg.general_cancel}" oncomplete="PF('confirmDelete').hide()"/>
				</div>
			</p:confirmDialog>		
		</h:form>

		<p:dialog id="editDialog1" header="#{msg.general_addedit_header}" modal="true" resizable="false"
			widgetVar="editDialog">
			<h:form id="editForm">
				<p:growl id="messages" />
				<h:panelGrid id="editTable" columns="2" styleClass="twoColTableTag twoColTableValue">
					<h:outputText value="#{msg.table_groupid}:"/>
					<h:inputText label="#{msg.table_groupid}" required="true" value="#{UserGroupsBean.selectedGroup.groupId}" id="groupId"
						disabled="#{!empty UserGroupsBean.selectedGroup.groupId}" style="width:100%"/>
					<h:outputText value="#{msg.table_groupname}:" />
					<h:inputText label="#{msg.table_groupname}" required="true" value="#{UserGroupsBean.selectedGroup.groupName}" id="groupName" style="width:100%"/>				
				</h:panelGrid>
				<p:commandLink oncomplete="PF('editDialog').hide()" update=":groupForm" rendered="#{!UserGroupsBean.createNew}">
					<h:outputText value="#{msg.table_roles}" />
				</p:commandLink>
				<br />
				<div style="float: right">
					<p:commandButton value="#{msg.general_save}" action="#{UserGroupsBean.saveGroup}" oncomplete="if (args.opSuccess) PF('editDialog').hide();"
						update=":groupForm" />
				</div>
			</h:form>
		</p:dialog>
	</ui:define>
</ui:composition>
